home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13604 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  51 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to use assert( )
  5. Date: 8 Apr 1996 22:49:16 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4kctosINNh7p@keats.ugrad.cs.ubc.ca>
  8. References: <4kc3k7$dur@orion.cybercom.net>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4kc3k7$dur@orion.cybercom.net>,
  12. John Nield <nield@cybercom.net> wrote:
  13. >I am interested in the use of the assert() library function (macro?).
  14. >Neither the FAQ, nor K&R seems to have anything about it. Could
  15. >someone help me with a breif explanation of its uses and mabye some
  16. >example code? 
  17.  
  18. K&R2 discusses the <assert.h> facility. Check the index under the A's.
  19.  
  20. >I'm just starting my first project big enough to split among many
  21. >people, and from the vague explanations I've heard, assert is supposed
  22. >to be a usefull way to cause errors when someone passes your code bad
  23. >values. How do I do this?
  24.  
  25. #include <assert.h>
  26.  
  27. .
  28. .
  29. .
  30.  
  31. assert( /* put here an expression that you expect to be true */ );
  32.  
  33. For example,  say you have a function that expects an integer X to be in 
  34. the range [1,1000]. You could do:
  35.  
  36. assert(X > 0 && X <= 1000);    /* lame imitation of range checking    */
  37.  
  38.  
  39.  
  40. To turn the assertions off, put the following statement _before_ your inclusion
  41. of the <assert.h> header:
  42.  
  43. #define NDEBUG
  44. #include <assert.h>
  45.  
  46. etc.
  47.  
  48.  
  49. -- 
  50.  
  51.